<?php

namespace oa\controllers;

use Yii;
use oa\models\Menu;
use oa\services\MenuService;

class MenuController extends BaseController
{
	public function actionIndex()
	{
        if ($this->request->isAjax) {
            return MenuService::getList();
        }
        return $this->fetch();
	}

	public function actionAdd()
	{
		if ($this->request->isPost) {
			$res = MenuService::save();
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err('提交失败', $res);
        }
        $pid = $this->input('pid');
		return $this->fetch(['pid' => $pid], 2);
	}

	public function actionEdit()
	{
		$id = $this->input('id');
		if ($this->request->isPost) {
			$res = MenuService::save($id);
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err($res);
        }
		$data = MenuService::one($id, 1);
		return $this->fetch(['data' => $data], 2);
	}

	public function actionRemove()
	{
		if ($this->request->isAjax) {
			if (MenuService::remove($this->input('id'))) {
				return $this->ok('删除成功');
			}
			return $this->err('删除失败');
        }
	}

	public function actionDisable()
	{
		if ($this->request->isAjax) {
			if (MenuService::disable($this->input('id'))) {
				return $this->ok('禁用成功');
			}
			return $this->err('禁用失败');
        }
	}

	public function actionEnable()
	{
		if ($this->request->isAjax) {
			if (MenuService::enable($this->input('id'))) {
				return $this->ok('启用成功');
			}
			return $this->err('启用失败');
        }
	}
	
}
